{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "737cbfa5",
   "metadata": {},
   "source": [
    "# 字典的方法\n",
    "## 1.1数据结构的常见方法  \n",
    "> len()查看长度  \n",
    "> in 查询成员是否存在:查询的是key是否存在    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4f8f9c83",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Build\",\n",
    "    1800004:\"Jeff\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6b82feab",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "79f9bf20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Build'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list[1800003]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c9fd5ce9",
   "metadata": {},
   "source": [
    "* 字典增加   \n",
    "> 1.先要查询要增加的key是否存在，以免新增加进来的key替换了原来的key  \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c7b84582",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "增加成功\n"
     ]
    }
   ],
   "source": [
    "if 1800006 in user_list:\n",
    "    print(\"该用户已存在，请更换key\")\n",
    "else:\n",
    "    user_list[1800006] = \"Bile\"\n",
    "    print(\"增加成功\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4e07d546",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jack',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Build',\n",
       " 1800004: 'Jeff',\n",
       " 1800006: 'Bile'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dde2fc4e",
   "metadata": {},
   "source": [
    "* 字典的修改  \n",
    "> 1.先查询要修改的key是否存在  \n",
    "> 2.如果不存在该key，可以需要考虑是否新增用户  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "c719ff4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "信息更新成功\n"
     ]
    }
   ],
   "source": [
    "if 1800001 in user_list:\n",
    "    user_list[1800001]=\"Apoo\"\n",
    "    print(\"信息更新成功\")\n",
    "else:\n",
    "    print(\"该key找不到\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5258edb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Apoo',\n",
       " 1800002: 'Marry',\n",
       " 1800003: 'Build',\n",
       " 1800004: 'Jeff',\n",
       " 1800006: 'Bile'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "074da15d",
   "metadata": {},
   "source": [
    "* 字典的查询： .get()可以查询到该key的结果   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "217c85c4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "print(user_list.get(18000009))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b0376aad",
   "metadata": {},
   "source": [
    "## 1.2字典的一般方法    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "da4307cd",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Build\",\n",
    "    1800004:\"Jeff\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8f44b84",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build', 1800004: 'Jeff'}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "22578558",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_items([(1800001, 'Jack'), (1800002, 'Marry'), (1800003, 'Build'), (1800004, 'Jeff')])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.items()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75f438a6",
   "metadata": {},
   "source": [
    "* 字典的循环遍历  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1a983782",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回顾：关于序列类型的数据 循环\n",
    "strings = \"hello world\"\n",
    "list = [18,19,18,20,19]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "33ae9865",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "h\n",
      "e\n",
      "l\n",
      "l\n",
      "o\n",
      " \n",
      "w\n",
      "o\n",
      "r\n",
      "l\n",
      "d\n"
     ]
    }
   ],
   "source": [
    "for i in strings:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ea026aec",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18\n",
      "19\n",
      "18\n",
      "20\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "for i in list:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "63687948",
   "metadata": {},
   "outputs": [],
   "source": [
    "user_list = {\n",
    "    1800001:\"Jack\",\n",
    "    1800002:\"Marry\",\n",
    "    1800003:\"Build\",\n",
    "    1800004:\"Jeff\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "6cddbebf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build', 1800004: 'Jeff'}\n",
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build', 1800004: 'Jeff'}\n",
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build', 1800004: 'Jeff'}\n",
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build', 1800004: 'Jeff'}\n"
     ]
    }
   ],
   "source": [
    "for i in user_list:\n",
    "    print(user_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "20b94378",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1800001 Jack\n",
      "1800002 Marry\n",
      "1800003 Build\n",
      "1800004 Jeff\n"
     ]
    }
   ],
   "source": [
    "for k,v in user_list.items():\n",
    "    print(k,v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "96d70c5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys([1800001, 1800002, 1800003, 1800004])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# dict.keys():取出全部的key\n",
    "user_list.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "64479826",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_values(['Jack', 'Marry', 'Build', 'Jeff'])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_list.values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "183663e5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1800004, 'Jeff')"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# popitem()  随机删除个\n",
    "user_list.popitem()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "fc1ec42c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1800001: 'Jack', 1800002: 'Marry', 1800003: 'Build'}\n"
     ]
    }
   ],
   "source": [
    "print(user_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71bc6b05",
   "metadata": {},
   "source": [
    "* fromkeys  \n",
    "*setdefault"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "7bc42925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Jack': 100, 'Marry': 100, 'Build': 100}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置系统人物的属性，所有人族 hp 10\n",
    "user_list.values()\n",
    "re_game = {}\n",
    "re_game.fromkeys(user_list.values(),100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "0a96eae3",
   "metadata": {},
   "outputs": [],
   "source": [
    "books = {'runoob':'菜鸟教程','google':'Google 搜索'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "daef6e53",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'菜鸟教程'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.setdefault('runoob','菜鸟阅读')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e8fd3b43",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'阿里阅读'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books.setdefault('alibaba','阿里阅读')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "be060e64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'runoob': '菜鸟教程', 'google': 'Google 搜索', 'alibaba': '阿里阅读'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "books"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32589d86",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
